package com.transformer.shobal.activitys.wigets;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import com.transformer.shobal.R;

/**
 * Created by tiny on 2015/1/25.
 */
public class ScrollerWigets extends RelativeLayout {

    private Context mContext;
    private Scroller mScroller;
    private ImageView imageView;
    private int startY;
    private int moveToX;
    private int moveToY;
    private int mLastDownY;
    private int mLastDownX;


    private void initView(){
        Interpolator interpolator = new BounceInterpolator();
        mScroller = new Scroller(mContext,interpolator);
        imageView = new ImageView(mContext);
/*        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);*/
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.drawable.ic_launcher);
        addView(imageView);
    }

    public void setImageViewSrc(int id){
        imageView.setImageResource(id);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("shobal", "onTouchEvent " + event.getAction()+",y="+event.getY()+",x="+event.getX());
        moveToX = (int) event.getX();
        moveToY = (int) event.getY();
        switch (event.getAction()){
            case  MotionEvent.ACTION_DOWN:
                return true;
            case  MotionEvent.ACTION_MOVE:
               scrollTo(-moveToX, -moveToY);
               break;
            case  MotionEvent.ACTION_UP:
                //scrollTo(0, 0);
                mScroller.startScroll(-moveToX,-moveToY,moveToX,moveToY,2000);
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    public ScrollerWigets(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public ScrollerWigets(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    public ScrollerWigets(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        initView();
    }

    @Override
    public void computeScroll() {
        Log.i("shobal","computeScroll");
//        super.computeScroll();
        if(mScroller.computeScrollOffset()){
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }
}
